package cn.singno.bob.jpa.utils;

import java.util.List;

import org.springframework.data.domain.Page;

import cn.singno.bob.bean.JsonMessage;
import cn.singno.bob.emun.BusinessCode;

public class JsonMessageUtils {


	
	/**
	 * 
	 * @param code
	 * @return
	 */
	public static JsonMessage getJsonMessage(BusinessCode code){
		JsonMessage jsonMessage = new JsonMessage();
		jsonMessage.setCode(code.getCode());
		jsonMessage.setMessage(code.getMessage());
		return jsonMessage;
	}
	
	/**
	 * 
	 * <p>描述：</p>
	 * <pre>
	 *    
	 * </pre>
	 * @param code
	 * @param message
	 * @return
	 */
	public static JsonMessage getJsonMessage(Integer code, String message){
		JsonMessage jsonMessage = new JsonMessage();
		jsonMessage.setCode(code);
		jsonMessage.setMessage(message);
		return jsonMessage;
	}
	
	/**
	 * 
	 * <p>描述：</p>
	 * <pre>
	 *    
	 * </pre>
	 * @param code
	 * @param obj
	 * @return
	 */
	public static JsonMessage getJsonMessage(BusinessCode code, Object obj){
		JsonMessage jsonMessage = new JsonMessage();
		jsonMessage.setCode(code.getCode());
		jsonMessage.setMessage(code.getMessage());
		jsonMessage.setObj(obj);
		return jsonMessage;
	}
	
	/**
	 * 
	 * <p>描述：</p>
	 * <pre>
	 *    
	 * </pre>
	 * @param code
	 * @param rows
	 * @return
	 */
	public static JsonMessage getJsonMessage(BusinessCode code, Page<?> rows){
		JsonMessage jsonMessage = new JsonMessage();
		jsonMessage.setCode(code.getCode());
		jsonMessage.setMessage(code.getMessage());
		jsonMessage.setRows(rows.getContent());
		setPage(jsonMessage, rows);
		return jsonMessage;
	}
	
	//设置分页
	private static void setPage(JsonMessage jsonMessage, Page<?> rows){
		jsonMessage.setPageCount(rows.getTotalPages());
		jsonMessage.setTotal(rows.getTotalElements());
		jsonMessage.setPageNow(rows.getNumber());
	}
	
	/**
	 * 
	 * <p>描述：</p>
	 * <pre>
	 *    
	 * </pre>
	 * @param code
	 * @param rows
	 * @return
	 */
	public static JsonMessage getJsonMessage(BusinessCode code, List<?> rows){
		JsonMessage jsonMessage = new JsonMessage();
		jsonMessage.setCode(code.getCode());
		jsonMessage.setMessage(code.getMessage());
		jsonMessage.setRows(rows);
		return jsonMessage;
	}
	
	/**
	 * 
	 * <p>描述：</p>
	 * <pre>
	 *    
	 * </pre>
	 * @param code
	 * @param rows
	 * @param obj
	 * @return
	 */
	public static JsonMessage getJsonMessage(BusinessCode code, List<?> rows, Object obj){
		JsonMessage jsonMessage = new JsonMessage();
		jsonMessage.setCode(code.getCode());
		jsonMessage.setMessage(code.getMessage());
		jsonMessage.setRows(rows);
		jsonMessage.setObj(obj);
		return jsonMessage;
	}
	
	/**
	 * 
	 * <p>描述：</p>
	 * <pre>
	 *    
	 * </pre>
	 * @param code
	 * @param rows
	 * @param obj
	 * @return
	 */
	public static JsonMessage getJsonMessage(BusinessCode code, Page<?> rows, Object obj){
		JsonMessage jsonMessage = new JsonMessage();
		jsonMessage.setCode(code.getCode());
		jsonMessage.setMessage(code.getMessage());
		jsonMessage.setRows(rows.getContent());
		jsonMessage.setObj(obj);
		setPage(jsonMessage, rows);
		return jsonMessage;
	}
	
	
	
}
